﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using WeiXinPayClass;
using WxPayAPI;
using Common;
using Model;
using DAL;

namespace UI
{
    public partial class notify_wx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RequestType == "POST")
            {
                try
                {
                    WxPayHelper helper = new WxPayHelper();
                    StreamReader reader = new StreamReader(Request.InputStream);
                    String xmlData = reader.ReadToEnd();
                    helper.ReceivePostXmlData(xmlData);
                    //common_util.WriteLog("Notify_接收post来的xmlData=" + xmlData);
                    if (helper.CheckSign())
                    {
                        Dictionary<string, string> dicBack = helper.GetParameter();//获取所有参数
                        if (dicBack != null && dicBack.Keys.Contains("return_code"))
                        {
                            if (dicBack["return_code"] == "SUCCESS")
                            {
                                //common_util.WriteLog("return_code=SUCCESS");
                                if (dicBack["result_code"] == "SUCCESS")
                                {
                                    //common_util.WriteLog("result_code=SUCCESS");
                                    string out_trade_no = dicBack["out_trade_no"];//商户订单号
                                    //common_util.WriteLog("out_trade_no=" + out_trade_no);
                                    //1.验证商户订单号是否被处理
                                    //2.处理过直接返回成功，否则返回
                                    //此处根据out_trade_no 处理业务数据
                                    //处理业务数据结束
                                    string[] paras = dicBack["attach"].Split('_');
                                    int userid = DataConvert.ToInt(paras[0]);
                                    string flag = paras[1];
                                    if (flag == "charge")
                                    {
                                        float total_fee = DataConvert.ToFloat(paras[2]);//
                                        ChargeInfo charge = new ChargeInfo()
                                        {
                                            UserID = userid,
                                            Money = total_fee,
                                            CreateDate = DateTime.Now
                                        };
                                        ChargeDAL.Save(charge);
                                        UserDAL.Charge(userid, total_fee);
                                    }
                                    else
                                    {
                                        ConfigInfo config = ConfigDAL.FindOneByID(1);
                                        UserDAL.UpdateRole(userid, config.BackMoney);
                                    }
                                    //common_util.WriteLog("Notify_验证签名成功");
                                    helper.SetReturnParameter("return_code", "SUCCESS");
                                    helper.SetReturnParameter("return_msg", "");
                                }
                            }
                            if (dicBack["return_code"] == "FAIL")
                            {
                                //common_util.WriteLog("Notify_验证签名成功");
                                helper.SetReturnParameter("return_code", "SUCCESS");
                                helper.SetReturnParameter("return_msg", dicBack["return_msg"]);
                                
                            }
                        }
                    }
                    else
                    {
                        //common_util.WriteLog("Notify_验证签名失败");
                        helper.SetReturnParameter("return_code", "FAIL");
                        helper.SetReturnParameter("return_msg", "签名失败");
                        Commons.Log("Notify_验证签名失败");
                    }
                    string xmlStr = helper.GetReturnXml();
                    //common_util.WriteLog("Notify_返回xml=" + xmlStr);
                    Response.ContentType = "text/xml";
                    Response.Clear();
                    Response.Write(xmlStr);
                    Response.End();
                }
                catch (Exception ex)
                {
                    Commons.Log(ex.Message);
                    //common_util.WriteLog("Notify_异常了" + ex);
                }
            }
        }
    }
}


